package cn.xutongxue.interview.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.http.server.HttpServerRequest;
import cn.xutongxue.interview.common.DeleteRequest;
import cn.xutongxue.interview.common.ResponseResult;
import cn.xutongxue.interview.constants.UserConstant;
import cn.xutongxue.interview.entity.User;
import cn.xutongxue.interview.entity.dto.user.LoginDto;
import cn.xutongxue.interview.entity.dto.user.RegisterDto;
import cn.xutongxue.interview.entity.dto.user.UserQueryDto;
import cn.xutongxue.interview.enums.Errors;
import cn.xutongxue.interview.exception.BusinessException;
import cn.xutongxue.interview.service.UserService;
import cn.xutongxue.interview.utils.ThrowUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户接口
 * @author Administrator
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "UserApi")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 登录
     * @param loginDto 登录信息
     * @return token字符串
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public ResponseResult<Boolean> login(@RequestBody @Validated LoginDto loginDto, HttpServletRequest request) {
        ThrowUtils.throwIf(ObjectUtils.isEmpty(loginDto), Errors.PARAMS_ERROR);
        return userService.login(loginDto, request);
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public ResponseResult<Boolean> register(@RequestBody @Validated RegisterDto registerDto) {
        ThrowUtils.throwIf(ObjectUtils.isEmpty(registerDto), Errors.PARAMS_ERROR);
        ThrowUtils.throwIf(!registerDto.getPassword().equals(registerDto.getCheckPassword()), new BusinessException(400502, "两次密码不一致"));
        return userService.register(registerDto);
    }

    /**
     * 注销登录
     * @return 注销登录结果
     */
    @ApiOperation("注销登录")
    @PostMapping("/logout")
    public ResponseResult<Boolean> logout() {
        StpUtil.checkLogin();
        StpUtil.logout();
        return ResponseResult.ok(true);
    }

    /**
     * 获取登录用户信息
     * @return 脱敏用户信息
     */
    @ApiOperation("获取登录用户信息")
    @PostMapping("/info")
    public ResponseResult<User> getLoginUserInfo(HttpServletRequest request) {
        return userService.getLoginUserInfo(request);
    }

    /**
     * 分页查询
     * @param queryDto 分页查询参数
     * @return 查询分页结果
     */
    @ApiOperation("获取用户列表")
    @PostMapping("/list")
    @SaCheckRole(UserConstant.USER_ROLE_ADMIN)
    public ResponseResult<Page<User>> listUser(@RequestBody UserQueryDto queryDto) {
        ThrowUtils.throwIf(queryDto == null, Errors.PARAMS_ERROR);
        Page<User> page = userService.listUserByPage(queryDto);
        return ResponseResult.ok(page);
    }

    /**
     * 添加用户
     * @param user 待添加的用户信息
     * @return 添加结果
     */
    @ApiOperation("添加用户")
    @PostMapping("/add")
    @SaCheckRole(UserConstant.USER_ROLE_ADMIN)
    public ResponseResult<Boolean> addUser(@RequestBody User user) {
        ThrowUtils.throwIf(user == null, Errors.PARAMS_ERROR);
        return userService.save(user) ? ResponseResult.ok(true) : ResponseResult.fail("添加用户失败");
    }

    /**
     * 删除用户
     * @param deleteRequest 待删除的用户ID
     * @return 删除结果
     */
    @ApiOperation("删除用户")
    @PostMapping("/delete")
    @SaCheckRole(UserConstant.USER_ROLE_ADMIN)
    public ResponseResult<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null, Errors.PARAMS_ERROR);
        return userService.removeById(deleteRequest.getId()) ? ResponseResult.ok(true) : ResponseResult.fail("删除用户失败");
    }

    /**
     * 修改用户信息
     * @param user 待修改的用户信息
     * @return 修改结果
     */
    @ApiOperation("更新用户信息")
    @PostMapping("/update")
    @SaCheckRole(UserConstant.USER_ROLE_ADMIN)
    public ResponseResult<Boolean> updateUser(@RequestBody User user) {
        ThrowUtils.throwIf(user == null || user.getId() == null, Errors.PARAMS_ERROR);
        return userService.updateById(user) ? ResponseResult.ok(true) : ResponseResult.fail("更新用户信息失败");
    }

    /**
     * 用户签到
     * @return 是否签到成功
     */
    @ApiOperation("用户签到")
    @PostMapping("/sign")
    public ResponseResult<Boolean> addUserSignIn() {
        Long currentUserId = StpUtil.getLoginIdAsLong();
        boolean result = userService.addUserSignIn(currentUserId);
        return ResponseResult.ok(result);
    }

    /**
     * 获取用户签到记录
     * @param year 获取年份
     * @return 获取到的签到记录
     */
    @ApiOperation("获取用户签到记录")
    @GetMapping("/get/sign")
    public ResponseResult<List<Integer>> getUserSignIn(@RequestParam Integer year) {
        Long currentUserId = StpUtil.getLoginIdAsLong();
        List<Integer> userSignInRecord = userService.getUserSignInRecord(currentUserId, year);
        return ResponseResult.ok(userSignInRecord);
    }
}
